package com.wanlian.candy.controller;

import com.wanlian.candy.controller.session.UserSessionInfo;
import com.wanlian.candy.controller.vo.CardPatchVo;
import com.wanlian.candy.controller.vo.CardVo;
import com.wanlian.common.ConstantCode;
import com.wanlian.common.PageCondition;
import com.wanlian.entity.Card;
import com.wanlian.entity.Product;
import com.wanlian.entity.User;
import com.wanlian.error.ErrorCode;
import com.wanlian.json.JsonResult;
import com.wanlian.json.JsonStringUtil;
import com.wanlian.model.ResultModel;
import com.wanlian.service.CardServices;
import com.wanlian.service.DeviceServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/card")
public class CardController {

    @Autowired
    public DeviceServices deviceServices;

    @Autowired
    public CardServices cardServices;


    /**
     * 添加电信卡
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String add(@RequestBody String data) {
        Card card = JsonStringUtil.readValue(data, Card.class);
        ResultModel resultModel = cardServices.doAdd(card);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 批量导入电信卡
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/add/patch", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String addPatch(@RequestBody String data) {
        CardPatchVo cardPatchVo = JsonStringUtil.readValue(data, CardPatchVo.class);
        List<Card> cards=new ArrayList<>();
        ResultModel resultModel = new ResultModel();
        int productId=0;
        if(cardPatchVo.getCards()!=null&&cardPatchVo.getCards().size()>0){
            cards=cardPatchVo.getCards();
            System.out.println(cards);
        }
        if(cardPatchVo.getProduct()!=null){
            productId=cardPatchVo.getProduct().getId();
        }
        if(cards.size()>0&&productId!=0){
            resultModel = cardServices.doAdd(cards,productId);
        }else{
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CARD_IMPORT_DEF);
        }
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 编辑电信卡
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/edit", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String edit(@RequestBody String data) {
        Card card = JsonStringUtil.readValue(data, Card.class);
        ResultModel resultModel = cardServices.doedit(card);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 查询电信卡
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/list/condition", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listByCondition(@RequestBody String data) {
        PageCondition pageCondition = JsonStringUtil.readValue(data, PageCondition.class);
        ResultModel resultModel = cardServices.queryCard(pageCondition);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 积分卡兑换
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/convert", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String convertCardByUser(@RequestBody String data) {
        CardVo cardVo = JsonStringUtil.readValue(data, CardVo.class);
        ResultModel resultModel = cardServices.doConverCardByUser(cardVo.getCard(), cardVo.getUser());
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * 运营平台
     * 查询某个用户下的卡片
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/user/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listCardByUser(@RequestBody String data) {
        PageCondition pageCondition=JsonStringUtil.readValue(data, PageCondition.class);
        int userId=Integer.parseInt((String)pageCondition.getByKey("userId"));
        ResultModel resultModel = cardServices.queryCardByUser(pageCondition.getPage(), userId);
        return new JsonResult().Serailze(resultModel);
    }

    /**
     * App平台
     * 查询某个用户下的卡片
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/app/user/list", method = {RequestMethod.POST}, produces = "application/json")
    @ResponseBody
    public String listCardByUserApp(@RequestBody String data, HttpSession session) {
        PageCondition pageCondition=JsonStringUtil.readValue(data, PageCondition.class);
        UserSessionInfo user = (UserSessionInfo) session.getAttribute(ConstantCode.USERINFO_KEY);
        int uid = user.getLoginUser().getId();
        ResultModel resultModel = cardServices.queryCardByUser(pageCondition.getPage(), uid);
        return new JsonResult().Serailze(resultModel);
    }

}
